<?php

namespace app\admin\controller;

use app\common\model\Ad as AdModel;
use app\common\model\AdCategory;
use app\common\controller\AdminBase;
use app\common\model\SiteCategory;

/**
 * 广告管理
 * Class AdminUser
 * @package app\admin\controller
 */
class Ad extends AdminBase
{
    protected $model;
    protected $categoryList;

    protected function _initialize()
    {
        parent::_initialize();
        $this->model = new AdModel;
        $this->category_model = new AdCategory();
        $this->categoryList = $this->category_model->select();
        $this->assign('category_list', $this->categoryList);
    }

    /**
     * 广告管理
     * @param string $keyword
     * @param int $page
     * @return mixed
     */
    public function index($keyword = '', $page = 1)
    {
        $page = isset($_REQUEST['page']) ? $_REQUEST['page'] : 1;
        $perpage = isset($_REQUEST['perpage']) ? $_REQUEST['perpage'] : 10;

        $status = isset($_REQUEST['status']) ? trim($_REQUEST['status']) : 0;
        $keyword = isset($_REQUEST['keyword']) ? trim($_REQUEST['keyword']) : '';

        $map = [];
        if ($keyword) {
            $map['title|link|poster_qq'] = ['like', "%{$keyword}%"];
        }

        if ($status) {
            $map['status'] = $status;
        }

        $list = $this->model->where($map)->order('location_id asc,sort desc')->paginate(10);
        $locations = array_column($this->categoryList->toArray(), 'title', 'location_id');

        foreach ($list as &$v) {
            if(isset($locations[$v['location_id']])){
                $v['location'] = $locations[$v['location_id']];
            }
        }
        $this->assign(array('status' => $status, 'keyword' => $keyword, 'perpage' => $perpage));

        return $this->fetch('index', ['list' => $list, 'keyword' => $keyword]);
    }

    public function toggle($id, $status, $name)
    {
        if ($this->request->isGet()) {
            if ($this->model->where('id', $id)->update([$name => $status]) !== false) {
                return json(array('code' => 200, 'msg' => '更新成功'));
            } else {
                return json(array('code' => 0, 'msg' => '更新失败'));
            }
        }

    }

    /**
     * 添加广告
     * @return mixed
     */
    public function add()
    {
        return $this->fetch();
    }

    /**
     * 保存广告
     */
    public function save()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $data['status'] = 1;

            if ($this->model->allowField(true)->save($data)) {
                return json(array('code' => 200, 'msg' => '添加成功'));
            } else {
                return json(array('code' => 0, 'msg' => '添加失败'));
            }

        }
    }

    /**
     * 编辑广告
     * @param $id
     * @return mixed
     */
    public function edit($id)
    {
        $info = $this->model->find($id);
        return $this->fetch('edit', ['ad' => $info]);
    }

    /**
     * 更新广告
     * @param $id
     */
    public function update()
    {

        if ($this->request->isPost()) {
            $data = $this->request->param();
            if ($this->model->allowField(true)->save($data, ['id' => $data['id']])) {
                return json(array('code' => 200, 'msg' => '修改成功'));
            } else {
                return json(array('code' => 0, 'msg' => '修改失败'));
            }
        }
    }

    public function alldelete()
    {
        $params = input('post.');
        $ids = implode(',', $params['ids']);
        $result = $this->model->destroy($ids);
        if ($result) {
            return json(array('code' => 200, 'msg' => '删除成功'));
        } else {
            return json(array('code' => 0, 'msg' => '删除失败'));
        }
    }

    /**
     * 删除广告
     * @param $id
     */
    public function delete($id)
    {
        if ($this->model->destroy($id)) {
            //$this->success('删除成功');
            return json(array('code' => 200, 'msg' => '删除成功'));
        } else {
            // $this->error('删除失败');
            return json(array('code' => 0, 'msg' => '删除失败'));
        }
    }
}